﻿<UserControl x:Class="Haplous.Presentation.GameArea" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:haplous="clr-namespace:Haplous.Presentation"
             xmlns:vm="clr-namespace:Haplous.Presentation.ViewModel"
             xmlns:conv="clr-namespace:Haplous.Presentation.Converters"
             mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Focusable="true"
             Unloaded="GameArea_OnUnloaded" >

    <UserControl.Resources>
        <ResourceDictionary>
            <!-- The view model providing the business logic behind this UI. -->
            <vm:GameAreaViewModel x:Key="ViewModel" />
            <!-- A converter for transalting between images and image source objects -->
            <conv:ImageToImageSourceConverter x:Key="ImageSourceConverter" />
            <conv:BrushConverter x:Key="BrushConverter" />
            <ImageBrush ImageSource="{Binding BackgroundImage, Source = {StaticResource ViewModel}, Converter={StaticResource ImageSourceConverter}}" Stretch="Fill" x:Key="BackgroundImageBrush" />

            <DataTemplate x:Key="GraphicBlockTemplate" DataType="{x:Type vm:GraphicBlockWrapper}" >
                <!-- This is the element that will define the dimensions of the element -->
                <Rectangle Width="{Binding Size.Width}" Height="{Binding Size.Height}" Visibility="{Binding Visibility}" Focusable="false" Opacity="{Binding Opacity}" >
                    <Rectangle.Fill>
                        <!-- And this will define the fill -->
                        <ImageBrush ImageSource="{Binding Image, Converter={StaticResource ImageSourceConverter}}" Stretch="{Binding Stretch}" TileMode="{Binding TileMode}"
                                    Viewport="{Binding Viewport}" ViewportUnits="{Binding ViewportUnits}" />
                    </Rectangle.Fill>
                </Rectangle>
            </DataTemplate>

            <DataTemplate x:Key="TextBlockTemplate" DataType="{x:Type vm:TextBlockWrapper}" >
                <!-- This is the element that will define the dimensions of the element -->
                <TextBlock Width="{Binding Size.Width}" Height="{Binding Size.Height}" Visibility="{Binding Visibility}" Focusable="false" Text="{Binding Text}" FontFamily="{Binding FontFamily}"
                           FontSize="{Binding FontSize}" Foreground="{Binding Foreground, Converter={StaticResource BrushConverter}}" Opacity="{Binding Opacity}"
                           TextAlignment="{Binding TextAlignment}" />
            </DataTemplate>

            <haplous:BlockTemplateSelector x:Key="BlockTemplateSelector" />

        </ResourceDictionary>
    </UserControl.Resources>

    <ItemsControl ItemsSource="{Binding Source={StaticResource ViewModel}, Path=VisualElements}" Background="{StaticResource BackgroundImageBrush}" SizeChanged="GameContainerSizeChanged"
                  x:Name="GameContainer" Focusable="false" ItemTemplateSelector="{StaticResource BlockTemplateSelector}" >

        <!-- Define the container in which the items will be displayed -->
        <ItemsControl.Template>
            <ControlTemplate>
                <Canvas Background="{StaticResource BackgroundImageBrush}" IsItemsHost="True" Focusable="false" />
            </ControlTemplate>
        </ItemsControl.Template>

        <ItemsControl.ItemContainerStyle>
            <Style TargetType="{x:Type ContentPresenter}" >
                <Setter Property="Canvas.Left" Value="{Binding Position.X}" />
                <Setter Property="Canvas.Top" Value="{Binding Position.Y}" />
                <Setter Property="Width" Value="{Binding Size.Width}" />
                <Setter Property="Height" Value="{Binding Size.Height}" />
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>

</UserControl>